<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/6/4
 * Time: 13:21
 * pv统计入库
 */
require_once dirname(__DIR__)."/common.php";
use app\lib\db\DbException;
use app\cls\tom\Queue;
use app\cls\tom\SysResource;
use app\cls\tom\Process;

$process = new Process(__FILE__);
$process->setProcessNum(5);
$process->initProcess();

$redis = SysResource::getRedis();
$db =  SysResource::getDB(true);

try{
    while(true){
        for($i = 1 ;$i<=5;$i++){
            for($j = 0 ;$j < 100;$j++){
                //test为测试，no_test为正式

                $PATH="no_test";
                if($PATH=="test"){
                    $key = REDIS_PREFIX."test_PV_".$i;
                    $key1 = REDIS_PREFIX."test_PV_1";
                    $key2 = REDIS_PREFIX."test_PV_2";
                    $key3 = REDIS_PREFIX."test_PV_3";
                    $key4 = REDIS_PREFIX."test_PV_4";
                    $key5 = REDIS_PREFIX."test_PV_5";
                }else if($PATH=="no_test"){
                    $key = REDIS_PREFIX."PV_".$i;
                    $key1 = REDIS_PREFIX."PV_1";
                    $key2 = REDIS_PREFIX."PV_2";
                    $key3 = REDIS_PREFIX."PV_3";
                    $key4 = REDIS_PREFIX."PV_4";
                    $key5 = REDIS_PREFIX."PV_5";
                }



                $dataStr = $redis->rPop($key);
                echo $dataStr."\n";
                $result =  json_decode($dataStr,TRUE);
                if(empty($result)){
                    break;
                }
                if($result&&in_array($result['xm'],['host'])){
                    $data['log_uid'] = isset($result['uid']) ? $result['uid'] : '';
                    $ip = isset($result['ip'])&&$result['ip'] ? explode(",",$result['ip']) : null;
                    $data['log_ip'] = $ip&&isset($ip[0])&&$ip[0] ? $ip[0] : '0.0.0.0';
                    $data['log_project'] = isset($result['xm'])&&$result['xm'] ? $result['xm'] : '0';
                    $data['log_position'] = isset($result['wz']) ? $result['wz'] : '';
                    $data['log_platform'] = isset($result['pt']) ? $result['pt'] : '';
                    $data['log_operate'] = isset($result['op']) ? $result['op'] : '';
                    $data['log_uniqueid'] = isset($result['uniqueid']) ? $result['uniqueid'] : '';
                    $data['log_other'] = isset($result['other']) ? $result['other'] : '';
                    $data['log_time'] = isset($result['time'])&&$result['time'] ? $result['time'] : date("Y-m-d H:i:s");
                    $data['log_addtime'] =date("Y-m-d H:i:s",time());
                    if(!empty($result['id'])){
                        $data['material_id'] =$result['id'];
                    }
                    if(!empty($result['source'])){
                        $data['source'] =$result['source'];
                    }
                    $res=$db->insert("data_logs_2018",$data);
                    if(!$res){
                        if($i==1){
                            $redis->lPush($key2,$dataStr);
                        }else if($i==2){
                            $redis->lPush($key3,$dataStr);
                        }else if($i==3){
                            $redis->lPush($key4,$dataStr);
                        }else if($i==4){
                            $redis->lPush($key5,$dataStr);
                        }else if($i==5){
                            $redis->lPush($key1,$dataStr);
                        }
                        $process->restartProcess();
                    }
                }
            }
        }
        sleep(1);
//        $process->checkProcessTime(true);
    }
}catch (Exception $e){
//    throw $e;
    $process->log($e->getMessage());
}
